<%@page import="java.sql.ResultSet"%>
<%@page import="ContentManagers.RoleManagers.PMManager"%>
<%@page import="ContentManagers.RoleManagers.AMManager"%>
<div id="accounting_manager_body" class="content_box">

                        <br>View Records
                        <br>
                        <br><smallerfont>by Product type
                        <form name ="ViewSalesPerType" method="post" onsubmit="" action="managing_index.jsp">
                        <select name="product_type">
                            <option value='0'>All Products</option>
                            <%
                                    try{   ResultSet rs = new PMManager().getProductTypes();

                                        if(rs == null){
                                            %> 
                                                Database connection error.
                                            <% }
                                        else
                                        {
                                            while(rs.next())
                                                {
                                                out.println("<option value='"+rs.getString("product_type_name")+"'>" + rs.getString("product_type_name") + "</option>");
                                                }
                                        }
                                    }catch(Exception e){ }
                            %>
                        </select><input type="submit" id="sales_per_type" name="SubmitSalesPerType" value="View"/>
                        <br>by Product Name:</smallerfont> 
                        <br><input class="inputbox" type="text" value="" name="productName" autocomplete ="off" style="width:95px;"/>
                        <input type="submit" id="sales_per_type" name="SubmitSalesPerProduct" value="Search"/>
                        </form></smallerfont>
                        

                        <div id="view_records">
                        <%
                                try{    if(request.getParameter("product_type").equals("0") == true)
                                    {   
                                        
                                        ResultSet rs = new AMManager().salesPerProductType(request.getParameter("product_type"));
                                        int i=1;
                                        if(rs == null)
                                        {%> 
                                        No records for this product type.
                                        <% }
                                        else
                                            {   while(rs.next())
                                                {
                                                    out.println("<table class=\"product_stats\" cellspacing=\"0\" cellpadding=\"4\">"
                                                            + "<tr><Td>Product type: <smallerfont> " 
                                                            + rs.getString("product_type_name") 
                                                            + "</smallerfont></td> <td colspan=\"2\"> Total sales: <smallerfont>" 
                                                            + rs.getString("type_total_sales") 
                                                            + "</smallerfont></td></tr>");
                                                            out.print("<tr><td>Product Name</td><td>Quantity Sold</td><td>Total Product Sales</td></tr>");

                                                            ResultSet rs2 = new AMManager().salesPerProduct(rs.getString("product_type_name"), true);
                                                            if(rs2 == null)
                                                            {   %> 
                                                            No records for this product type.
                                                            <% }
                                                            else
                                                                {   while(rs2.next())
                                                                    {
                                                                        out.println("<tr><td><div class=\"productName\">" 
                                                                                + rs2.getString("product_name") 
                                                                                + "</div></td><td><div class=\"productSold\">" 
                                                                                + rs2.getString("quantity_purchased") 
                                                                                + "</div></td><td><div class=\"productSales\">" 
                                                                                + rs2.getString("product_total_sales") 
                                                                                + "</div></td></tr>");
                                                                    }
                                                                }
                                                            out.print("</table><br><br>");
                                                            i++;
                                                    }
                                            }
                                }

                                    }catch(Exception e){ }
                            %>
                               
                        </table>
                            <br><br>

                            <%
                                try{
                                    if(request.getParameter("product_type").equals("0") == false)
                                    {   
                                        
                                        ResultSet rs = new AMManager().salesPerProductType(request.getParameter("product_type"));
                                        if(rs == null)
                                        {%> 
                                        No records for this product type.
                                        <% }
                                        else
                                            {   while(rs.next())
                                                {
                                                    out.println("<table class=\"product_stats\" cellspacing=\"0\" cellpadding=\"4\">"
                                                            + "<tr><Td>Product type: <smallerfont> " 
                                                            + rs.getString("product_type_name") 
                                                            + "</smallerfont></td> <td colspan=\"2\"> Total sales: <smallerfont>" 
                                                            + rs.getString("type_total_sales") 
                                                            + "</smallerfont></td></tr>");
                                                }
                                            }
                                        
                                        
                                        
                                        out.print("<tr><td>Product Name</td><td>Quantity Sold</td><td>Total Product Sales</td></tr>");

                                        ResultSet rs2 = new AMManager().salesPerProduct(request.getParameter("product_type"), false);
                                        if(rs2 == null)
                                        {%> 
                                        No records for this product type.
                                        <% }
                                        else
                                            {   while(rs2.next())
                                                {
                                                    out.println("<tr><td><div class=\"productName\">" 
                                                            + rs2.getString("product_name") 
                                                            + "</div></td><td><div class=\"productSold\">" 
                                                            + rs2.getString("quantity_purchased") 
                                                            + "</div></td><td><div class=\"productSales\">" 
                                                            + rs2.getString("product_total_sales") 
                                                            + "</div></td></tr>");
                                                }
                                            }
                                        out.print("</table>");
                                        }

                                    }catch(Exception e){ }
                            %>


                        </div>
</div>
